package com.sunspring.zdcall.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

/**
 * @author wulei
 * @date 2019-01-14
 **/
@Slf4j
public class FileUtil {


    /**
     * 静态资源前缀
     */
    public static final String PATTERNS = "/sys/file/";

    /**
     * 本地路径
     */
    public static final String RESOURCES_LOCATION = "D:\\中迪排队叫号系统\\";


    /**
     * 服务器地址
     */
    private final static String SERVER_URL = "http://192.168.0.117:8080" + PATTERNS;

    /**
     * logo名称
     */
    private final static String LOGO = "logo";


    /**
     * 拼接地址
     * @param name 数据库文件名
     * @return 绝对地址
     */
    public static String spliceAddress(String name) {
        return SERVER_URL + name;
    }

    public static String saveToLocal(MultipartFile multipartFile) throws IOException {
        if (multipartFile == null) {
            return null;
        }
        String fileName = LOGO + ".png";
        File file = new File(RESOURCES_LOCATION,fileName);
        //删除旧logo
        if(file.exists()) {
            file.delete();
        }
        multipartFile.transferTo(file);
        return fileName;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        InetAddress ia=null;
        try {
            ia=InetAddress.getLocalHost();

            String localname=ia.getHostName();
            String localip=ia.getHostAddress();
            System.out.println("本机名称是："+ localname);
            System.out.println("本机的ip是 ："+localip);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
